package com.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.pojo.SysUser;
import com.service.UserService;
import com.util.ConstUtils;
import com.vo.DataVO;
import com.vo.UserVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.security.RolesAllowed;
import java.security.Principal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author mornd
 * @date 2020/11/12 - 14:06
 */
@RestController
@RequestMapping("/user")
//表示携带了超级管理员和管理员权限的用户才能访问
@RolesAllowed({"ROLE_ADMIN", "ROLE_SUPERADMIN"})//表示携带指定权限的用户才可以访问 可设置多个 表示或者
//@PreAuthorize("hasAuthority('')")
public class UserController {
    @Autowired
    private UserService userService;

    //查询用户集合
    @RequestMapping("/list")
    public DataVO<SysUser> findUser(UserVo userVo, Principal principal) {
        SysUser loginUser = userService.findUserInfoByUserName(principal.getName());
        //如果当前登录的用户userType为2则是普通用户 则不可看到管理员的数据
        /*x if(loginUser!=null && loginUser.getUserType().equals(ConstUtils.DEFAULT_USERTYPE)){
            userVo.setUserType(ConstUtils.DEFAULT_USERTYPE);
        }*/
        PageHelper.startPage(userVo.getPage(), userVo.getLimit());
        List<SysUser> userList = userService.findUserList(userVo);
        //将创建和修改的id改为用户名称
        for (SysUser sysUser : userList) {
            //将用户id改成用户名
            SysUser createUserById = userService.findUserById(sysUser.getCreatedBy());
            //更改创建人
            sysUser.setCreatedByStr(createUserById.getRealName());
            //更改修改人
            if (sysUser.getModifyBy() != null) {
                SysUser modifyUserById = userService.findUserById(sysUser.getModifyBy());
                sysUser.setModifyByStr(modifyUserById.getRealName());
            }
        }
        PageInfo<SysUser> pageInfo = new PageInfo<SysUser>(userList);
        return new DataVO<SysUser>(pageInfo.getTotal(), pageInfo.getList());
    }

    //添加用户
    @RequestMapping("/add")
    public Map<String, Object> addUser(SysUser sysUser, Principal principal) {
        return userService.addUser(sysUser, principal);
    }

    //修改用户
    @RequestMapping("/update")
    public Map<String, Object> updateUser(SysUser sysUser, Principal principal) {
        return userService.updateUser(sysUser, principal);
    }

    //删除用户
    @RequestMapping("/del")
    public Map<String, Object> delUser(Integer userId) {
        Map<String, Object> maps = new HashMap<String, Object>();
        if (userService.deleteUser(userId)) {
            maps.put(ConstUtils.SUCCESS, true);
            maps.put(ConstUtils.MESSAGE, "删除用户成功！");
        } else {
            maps.put(ConstUtils.SUCCESS, false);
            maps.put(ConstUtils.MESSAGE, "删除用户失败，请重试！");
        }
        return maps;
    }

    @RequestMapping("/addRoleByuserId")
    //给用户添加角色
    public Map<String, Object> addRoleByuserId(String ids, Integer userId, Principal principal) {
        Map<String, Object> maps = new HashMap<String, Object>();
        String[] splitIds = ids.split(",");
        List<Integer> list = new ArrayList<Integer>();
        for (String id : splitIds) {
            list.add(Integer.valueOf(id));
        }
        if (userService.addRoleByuserId(list, userId, principal)) {
            maps.put(ConstUtils.SUCCESS, true);
            maps.put(ConstUtils.MESSAGE, "分配角色成功!");
        } else {
            maps.put(ConstUtils.SUCCESS, false);
            maps.put(ConstUtils.MESSAGE, "分配角色失败，请重试!");
        }
        return maps;
    }
}
